var express = require("express");
var router = express.Router();
var pomelo = require("pomelo");
var querystring = require('querystring');
var logger = require('pomelo-logger').getLogger(__filename);

router.post("/wxRecharge", function(req, res) {
    logger.info("receive auth server recharge request. ip: %s", req.ip);
    var postData = '';
    req.setEncoding("utf8");
    req.on('data', function (chunk) {
        postData += chunk;
    });
    req.on('end', function () {
        postData = JSON.parse(postData);
        logger.info("post data after JSON.parse:", postData);
        pomelo.app.rpc.account.rechargeRemote.recharge(null, postData, function(data) {
            logger.info("rechargeRemote.recharge callback: %s" + JSON.stringify(data));
            res.write(JSON.stringify(data));
            res.end();
        });
    });
});

router.post("/sftRecharge", function(req, res) {
    logger.info("receive auth server recharge request. ip: %s", req.ip);
    var postData = '';
    req.setEncoding("utf8");
    req.on('data', function (chunk) {
        postData += chunk;
    });
    req.on('end', function () {
        postData = JSON.parse(postData);
        logger.info("post data after JSON.parse:", postData);
        pomelo.app.rpc.account.rechargeRemote.shengfutongRecharge(null, postData, function(data) {
            logger.info("rechargeRemote.recharge callback: %s" + JSON.stringify(data));
            res.write(JSON.stringify(data));
            res.end();
        });
    });
});

router.get("/test", function(req, res) {
    logger.info("test rerecharge url");
    res.write("test ok");
    res.end();
});

module.exports = router;